package com.hnucm.house.ui.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.graphics.ColorUtils;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.hnucm.house.R;
import com.hnucm.house.base.BaseFragment;
import com.hnucm.house.base.BasePresenter;
import com.qmuiteam.qmui.widget.tab.QMUITabSegment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class ViewFragment extends BaseFragment {

    @BindView(R.id.view_tab)
    QMUITabSegment mSlidingTabLayout;

    @BindView(R.id.view_viewpager)
    ViewPager mViewPager;

    private int mCurTab;

    private Context mContext;

    private List<String> mTabNames = new ArrayList<>();

    private ArrayList<Fragment> mFragmentArrayList = new ArrayList<>();


    public static ViewFragment getInstance(){
        ViewFragment viewFragment = new ViewFragment();
        return viewFragment;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_view;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    protected void init() {
        mContext = getActivity().getApplicationContext();
        initStatusBar();
        mCurTab = 0;
        initFragment();
        intiViewPager();
    }
    private void initStatusBar() {
        getActivity().getWindow().addFlags(
                WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        getActivity().getWindow().setStatusBarColor(Color.TRANSPARENT);
        if (ColorUtils.calculateLuminance(com.blankj.utilcode.util.ColorUtils.getColor(R.color.white)) >= 0.5) {
            getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        } else {
            getActivity().getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
        }
    }

    private void initFragment() {
        mTabNames.add(0,"楼市行情");
        mTabNames.add(1,"大咖说房");
        mTabNames.add(2,"避坑指南");
        mFragmentArrayList.add(0,new VidieFragment());
        mFragmentArrayList.add(1,new NewHouseFragment());
        mFragmentArrayList.add(2,new NewHouseFragment());
    }
    private void intiViewPager() {
        mViewPager.setAdapter(new FragmentStatePagerAdapter(getChildFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return mFragmentArrayList.get(position);
            }

            @Override
            public int getCount() {
                return mFragmentArrayList == null ? 0: mFragmentArrayList.size();
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return mTabNames.get(position);
            }
        });

        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                mCurTab = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mSlidingTabLayout.setupWithViewPager(mViewPager);
    }
    @Override
    protected BasePresenter createPresenter() {
        return null;
    }
}
